package cn.xxy.converter;

import cn.xxy.TypeConverter;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.LocalTime;
/**
 * @author xxy
 * @date 2024/11/20
 */
public class LocalTimeConverter implements TypeConverter {
    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalTime.class);
    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        if(value instanceof LocalTime) {
            return value;
        }
        else if(value instanceof LocalDateTime) {
            return ((LocalDateTime)value).toLocalTime();
        }
        else if(value instanceof Time) {
            return ((Time)value).toLocalTime();
        }
        else if(value instanceof Timestamp) {
            return ((Timestamp)value).toLocalDateTime().toLocalTime();
        }
        return  null;
//        return switch (value) {
//            case LocalTime localTime -> localTime;
//            case LocalDateTime localDateTime -> localDateTime.toLocalTime();
//            case Time time -> time.toLocalTime();
//            case Timestamp timestamp -> timestamp.toLocalDateTime().toLocalTime();
//            default -> null;
//        };
    }
}
